﻿Imports Microsoft.VisualStudio.DebuggerVisualizers
Imports System.Collections.Generic
Imports System.Data.Objects
Imports System.Windows.Forms

<Assembly: DebuggerVisualizer(GetType(ObjectContextVisualizer), GetType(ObjectContextVisualizerObjectSource), _
 Target:=GetType(ObjectContext), Description:="ObjectContext visualizer")> 

Public Class ObjectContextVisualizer
    Inherits DialogDebuggerVisualizer


    Protected Overrides Sub Show(ByVal windowService As IDialogVisualizerService, ByVal objectProvider As IVisualizerObjectProvider)

        Dim ds As New DataSet()
        ds.ReadXml(objectProvider.GetData(), XmlReadMode.ReadSchema)

        Dim dtEntities As DataTable = ds.Tables(Constants.TABLE_ENTITIES)

        Using form As New ObjectContextVisualizerForm
            form.dgvData.DataSource = dtEntities
            form.dgvData.Columns(0).Width = 250
            form.dgvData.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            form.dgvData.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            form.dgvData.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            form.dgvData.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

            Dim total As Integer = (From v In dtEntities _
                                    Select Convert.ToInt32(v(1)) + Convert.ToInt32(v(2)) + _
                                    Convert.ToInt32(v(3)) + Convert.ToInt32(v(4))).Sum()

            form.lblCount.Text = String.Format("{0} entities", total)
            form.ShowDialog()
        End Using
    End Sub

End Class
